class OrgList extends Array {
    static idSeed = 1
    constructor(idSeed = 1) {
        super();
        this.init()
        OrgList.idSeed = idSeed
    }
    init() {
        this.addOrg('9')
        this.addOrg('10')
        this.addOrg('11')
    }
    addOrg(orgName) {
        this.push({
            id: OrgList.idSeed,
            name: orgName
        })
        OrgList.idSeed++
    }
    findOrgById(id) {
        let res = this.find(e => e.id === id)
        return res
    }
}
class Donation {
    constructor(id, username, orgId, money, date) {
        this.id = id
        this.username = username
        this.orgId = orgId
        this.money= money
        this.date = date
    }
}
class DonationList extends Array {
    constructor(pageIndex = 1, pageSize = 5) {
        super()
        this.init()
        this.searchOrgId = -1
        this.searchResList = this
        this.pageIndex = pageIndex
        this.pageSize = pageSize
        this.pageCount = 0
        this.countPage()
    }
    countPage() {
        this.pageCount = Math.ceil(this.searchResList.length / this.pageSize)
    }
    init() {
        this.addDonation("李连杰", 1, 10000)
        this.addDonation("星爷", 1, 10000);
        this.addDonation("甄子丹", 1, 10000);
        this.addDonation("周润发", 1, 10000);
        this.addDonation("成龙", 2, 20000)
        this.addDonation("徐静蕾", 2, 2220000);
        this.addDonation("陈光标", 2, 1000);
        this.addDonation("梁朝伟", 2, 20000);
        this.addDonation("老邹", 2, 20000);
        this.addDonation("马云", 3, 2000000);
        this.addDonation("洪金宝", 3, 2000000);
        this.addDonation("刘德华", 3, 500000);
        this.addDonation("易烊千玺", 3, 2000000);
        this.addDonation("机器猫", 3, 2000000);
        this.addDonation("阿童木", 3, 2000000);
        this.addDonation("黑猫警长", 3, 2000000);
        this.addDonation("黑马程序员", 3, 2000000);
    }
    getPagedList(pageIndex) {
        if (pageIndex) this.pageIndex = pageIndex;
        else pageIndex = this.pageIndex
        let beginI = (pageIndex - 1) * this.pageSize
        let endI = pageIndex * this.pageSize - 1
        endI = endI > this.searchResList.length - 1 ? this.searchResList.length - 1 : endI
        let pagedList = []
        for (let i = beginI; i < endI; i++) {
            pagedList.push(this.searchResList[i])
        }
        return pagedList
    }
    getNextPage() {
        if (this.pageIndex === this.pageCount) {
            return alert('Уже последняя страница')
        }
        this.pageIndex++
        return this.getPagedList(this.pageIndex)
    }
    getPrevPage() {
        if (this.pageIndex === 1) {
            return alert('Уже первая страница')
        }
        this.pageIndex--
        return this.getPagedList(this.pageIndex)
    }
    addDonation(username, orgId, money, date) {
        date = date ? new Date(date) : new Date()
        date = date.dateFormat("YYYY-mm-dd")
        let newId = this.length > 0 ? this[this.length - 1].id + 1 : 1
        let donation = new Donation(newId, username, orgId, money, date)
        this.push(donation)
        this.searchOrgId > -1 && this.searchResList.some(e => e.orgId == orgId) && this.searchResList.push
        this.searchResList && this.countPage()
    }
    doSearch(orgId) {
        this.searchOrgId = orgId
        this.searchResList = []
        if (orgId > 0) {
            this.forEach(e => {
                if (e.orgId == orgId) this.searchResList.push(e)
            })
        } else {
            this.searchResList = this
        }
        this.countPage()
        return this.getPagedList(1)
    }
    deleteById(id) {
        id = parseInt(id)
        let index = this.findIndex(e => e.id === id)
        index !== -1 && this.splice(index, 1)
        index = this.searchResList.findIndex(e => e.id === id)
        index !== -1 && this.searchResList.splice(index, 1)
        this.countPage()
    }
    findById(id) {
        let res = this.find(e => e.id === id)
        return res
    }
    getListByOrgId(id) {
        return this.filter(e => e.orgId === id)
    }
}
Date.prototype.dateFormat = function (fmt) {
    let ret;
    const opt = {
        "Y+": this.getFullYear().toString(),
        "m+": (this.getMonth() + 1).toString(),
        "d+": this.getDate().toString(),
        "H+": this.getHours().toString(),
        "M+": this.getMinutes().toString(),
        "S+": this.getSeconds().toString()
    }
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        }
    }
    return fmt;
}
String.prototype.toNumber = function () {
    return Number(this)
}